package com.ccapton.dragviewdemo.tag;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import java.util.Timer;
import java.util.TimerTask;

import static android.content.Context.INPUT_METHOD_SERVICE;

/**
 * Created by wenshi on 2019/3/12.
 * Description
 */
public class RandomDragTagLayout extends RelativeLayout {
    private String TAG = "RandomDragTagView";
    public RandomDragTagLayout(Context context) {
        this(context, null);
    }

    public RandomDragTagLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RandomDragTagLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    /**
     * 添加标签
     *
     * @param text           标签文本
     * @param x              相对于父控件的x坐标百分比
     * @param y              相对于父控件的y坐标百分比
     * @param isShowLeftView 是否显示左侧标签
     */
    public boolean addTagView(String text,String type,int width,int height,int color,int size, final float x, final float y, boolean isShowLeftView) {

        final RandomDragTagView tagView = new RandomDragTagView(getContext());
        tagView.setSelected(true);
        tagView.initTagView(text,type,width,height,color,size, x * getWidth(), y * getHeight(), isShowLeftView);
        tagView.setOnRandomDragListener(new RandomDragTagView.OnRandomDragListener() {
            @Override
            public void onStartDrag() {
                Log.e(TAG, "onStartDrag: " );
            }

            @Override
            public void onStopDrag() {
                Log.e(TAG, "onStopDrag: " );
            }

            @Override
            public void onClick(boolean isSelect, final RandomDragTagView tagView, EditText editText) {
                Log.e(TAG, "onClick: " +isSelect);
                if(onSizeChangeListener!=null){
                    onSizeChangeListener.onCLick();
                }
                if(tagView.getTagType().equals(TagModel.TEXT_TYPE)){
                    openKeyboard(editText);
                    editText.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {

                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            tagView.setTagText(s.toString());
                        }
                    });
                }

            }

            @Override
            public void onSelectTouch(EditText editText) {
                Log.e(TAG, "onSelectTouch: " );
                setAllNotSelect(tagView);
                if(onSizeChangeListener!=null){
                    onSizeChangeListener.onSizeChange(tagView.getSize());
                }
                if(tagView.getTagType().equals(TagModel.TEXT_TYPE)){
                    closeKeyboard(editText);
                }
            }
        });
        addView(tagView, new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        setAllNotSelect(tagView);
        return true;
    }

    public void setAllNotSelect(RandomDragTagView dragTagView){
        for (int i = 0; i < getChildCount(); i++) {
            View childAt = getChildAt(i);
            childAt.setSelected(childAt==dragTagView);
            RandomDragTagView tagView = (RandomDragTagView) childAt;
            tagView.refreshSelectStatus();
            childAt.invalidate();
        }
    }

    public RandomDragTagView getSelectView(){
        RandomDragTagView dragTagView = null;
        for (int i = 0; i < getChildCount(); i++) {
            View childAt = getChildAt(i);
            if(childAt.isSelected()){
                dragTagView = (RandomDragTagView) childAt;
                break;
            }
        }
        return dragTagView;
    }

    private OnSizeChangeListener onSizeChangeListener;

    public void setOnSizeChangeListener(OnSizeChangeListener onSizeChangeListener) {
        this.onSizeChangeListener = onSizeChangeListener;
    }

    public interface OnSizeChangeListener{
        void onSizeChange(float size);
        void onCLick();
    }


    /**
     * 打开键盘
     *
     * @param editText 操作的输入框
     */
    public void openKeyboard(final EditText editText) {
        //设置可获得焦点
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        //请求获得焦点
        editText.requestFocus();
        //调用系统输入法
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) editText
                        .getContext().getSystemService(
                                INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(editText, 0);
            }
        }, 100);
    }



    /**
     * 关闭键盘
     *
     * @param editText 操作的输入框
     */
    public static void closeKeyboard(EditText editText) {
        //关闭键盘
        InputMethodManager imm = (InputMethodManager) editText
                .getContext().getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }
}
